function numericCheck(checkedValue)
{
	var nonums = /^[0-9]*$/;
	if (nonums.test(checkedValue))
	{
		return true;
	}
	return false;
}

function creditPtsCheck(checkedValue)
{
	var testString = /^([0-9]+\.[0-9]+|[0-9]+)$/;
	if (testString.test(checkedValue))
	{
		return true;
	}
	return false;
}

function checkCourseForm()
{
	testedForm = document.getElementById("courseForm");
	
	// Emptiness validation
	if (testedForm.elements["Course name"].value.length == 0)
	{
		alert("Please insert a unique course name!");
		return false;
	}
	
	if (testedForm.elements["Description"].value.length == 0)
	{
		alert("Please insert some description about the course!");
		return false;
	}
	
	if (testedForm.elements["Credit points"].value.length == 0)
	{
		alert("Please insert credit points!");
		return false;
	}

	if (testedForm.elements["Student capacity"].value.length == 0)
	{
		alert("Please insert the number of students that call enroll!");
		return false;
	}

	if (testedForm.elements["Group Number"].value.length == 0)
	{
		alert("Please insert the course's group number!");
		return false;
	}

	if (false == creditPtsCheck(testedForm.elements["Credit points"].value))
	{
		alert("Please insert credit points in the format of X.Y or Z");
		return false;
	}
	
	// Checking for numeric values characters
	if (false == numericCheck(testedForm.elements["Group Number"].value))
	{
		alert("Please insert numeric values in the group number");
		return false;
	}
	
	if (false == numericCheck(testedForm.elements["Student capacity"].value))
	{
		alert("Please insert numeric values in the number of students which can enroll");
		return false;
	}

	// Good values test
	if (parseFloat(testedForm.elements["Credit points"].value) <= 0)
	{
		alert("Non acceptable number of credit points!");
		return false;
	}
	
	if (parseInt(testedForm.elements["Student capacity"].value) <= 0)
	{
		alert("Invalid student capacity!");
		return false;
	}
	
	if (parseInt(testedForm.elements["Group Number"].value) <= 0)
	{
		alert("Invalid group number!");
		return false;
	}
	
	// Checking the sessions
	// Lets see if there's at least one that has all the values...
	var i = 0;
	var flag = false;
	for (i = 0 ; i < 5 ; i++)
	{
		if (testedForm.elements["day_" + i].value != '0' &&
			testedForm.elements["starting_time_" + i].value != '0' &&	
			testedForm.elements["duration_" + i].value != '0')
		{
			flag = true;
		}
	}
	
	if (false == flag)
	{
		alert("You haven't entered at least one full session, please retry!");
		return false;
	}
	
	var sessions = [0,0,0,0,0];
	// Now lets check if there are incomplete ones, or have bad values
	for (i = 0 ; i < 5 ; i++)
	{
		if (testedForm.elements["day_" + i].value == '0' &&
			testedForm.elements["starting_time_" + i].value == '0' &&	
			testedForm.elements["duration_" + i].value == '0')
		{
			// It's empty... OKZ
			continue;
		}
		
		// Checking if the times are OK
		// No time to make one box connected dynamically to the other...
		if (testedForm.elements["day_" + i].value != '0' &&
			testedForm.elements["starting_time_" + i].value != '0' &&	
			testedForm.elements["duration_" + i].value != '0')
		{
			if (parseInt(testedForm.elements["starting_time_" + i].value) + 
				parseInt(testedForm.elements["duration_" + i].value) > 18)
			{
				alert("Session " + i + " can't end after 18:00!");
				return false;
			}
			sessions[i] = 1;
			continue;
		}
		
		// This session is bad, only half of the fields are filled?
		return false;
	}
	
	// Lets check for overlaps
	for (i = 0 ; i < 5 ; i++)
	{
		if (sessions[i] == 0)
		{
			continue;
		}
		
		var j = 0;
		for (j = i + 1 ; j < 5 ; j++)
		{
			if (sessions[j] == 0)
			{
				continue;
			}
			
			if (testedForm.elements["day_" + i].value != 
				testedForm.elements["day_" + j].value)
			{
				continue;
			}

			var aStart = parseInt(testedForm.elements["starting_time_" + i].value); 
			var aDuration = parseInt(testedForm.elements["duration_" + i].value);
			var bStart = parseInt(testedForm.elements["starting_time_" + j].value); 
			var bDuration = parseInt(testedForm.elements["duration_" + j].value);
			var aFinish = aStart + aDuration;
			var bFinish = bStart + bDuration;
					
			if ((aStart > bStart && aStart < bFinish) ||
				(aFinish > bStart && aFinish < bFinish) ||
				(bStart > aStart && bStart < aFinish) ||
				(bFinish > aStart && bFinish < aFinish) ||
				(aStart == bStart && aFinish == bFinish))
			{
				alert("Sessions " + i + " and " + j + " overlap, please change at least one of them!");
				return false;
			}
		}
	}
	
	// Since it's a float, we must encode it with the escape function to allow X.Y input
	testedForm.elements["Credit points"].value = encodeURIComponent(testedForm.elements["Credit points"].value);
	
	return true;
}